Registering a Callback
To handle an event you simply register an event handler callback function with the component through one of event registering functions.
For example to register and action event handler with a button you do it like this:
button.onAction(e=>{alert(e.target.text());}); // Shows a dialog windows with button's text
or, instead of arrow function e=>{} use a regular function(e){} to make this variable reference to the component that triggered the event:
button.onAction(function(e){
alert(this.text()); // Shows a dialog windows with button's text
});
Unregistering the Callback
To unregister the callback from the event you pass that callback to the same method, but also pass true as a second parameter to specify that you want to remove this handler from that event.
Here is an example of first registering an event, and then removing the handler:
let callback = e => {console.log(e.target.text());};
button.onAction(callback); // registers the callback
button.onAction(callback, true); // unregisters the callback
let _callbacks = button.onAction(); // gets a list of all registered callbacks for this event for this button
What to watch out for
Event handling in the swing-ui GUI module works differently compared to languages like Java, .NET, or C++.
When a component property (for example, its position) is changed, an event is generated and added to an event queue. The corresponding event handler callback is not executed immediately — it runs only after the current execution cycle finishes.
This design prevents infinite loops caused by synchronous event firing (execution blocking), but it also means events can’t be handled until the ongoing execution is complete.